﻿
(function () {
    appModule.controller('common.views.notificationManage.createOrEditModal', [
        '$scope', '$uibModalInstance', 'abp.services.app.notification', 'notificationId', 'abp.services.app.area', '$http', 'textAngularManager',
        function ($scope, $uibModalInstance, notificationService, notificationId, areaService, $http, textAngularManager) {
            var vm = this;
            vm.saving = false;
            //首先将disputeType数据设置为null
            vm.disputeType = null;

            vm.requestParams = {
                townID: 0,
                areaID: 0,
                villageID: 0
            };

            //---------------------------------------------------------------------------------------------------------------------------

            //vm.m = [];
            //vm.checked = [];
            //vm.selectAll = function () {
            //    if (vm.select_all) {
            //        vm.select_one = true;
            //        vm.checked = [];
            //        angular.forEach(vm.userList, function (i, index) {
            //            vm.checked.push(i.id);
            //            vm.m[i.id] = true;
            //        })
            //    } else {
            //        vm.select_one = false;
            //        vm.checked = [];
            //        vm.m = [];
            //    }
            //};

            //vm.selectOne = function (select) {
            //    angular.forEach(vm.m, function (i, id) {
            //        var index = vm.checked.indexOf(id);
            //        if (i && index === -1) {
            //            vm.checked.push(id);
            //        } else if (!i && index !== -1) {
            //            vm.checked.splice(index, 1);
            //        };
            //    });
            //    if (vm.userList.length === vm.checked.length) {
            //        vm.select_all = true;
            //    } else {
            //        vm.select_all = false;
            //    }
            //}

            //---------------------------------------------------------------------------------------------------------------------------


            //触发保存方法
            vm.save = function () {
                //验证
                //if (vm.checked.length == 0) {
                //    abp.message.warn('请至少选择一个用户！', '提示');
                //    return;
                //}
                if (vm.requestParams.townID == 0 || vm.requestParams.townID == '0') {
                    abp.message.warn('请至少选择用户！', '提示');
                    return;
                }

                vm.saving = true;
                //var clientIDs = '';
                //for (var i = 0; i < vm.checked.length; i++) {
                //    for (var j = 0; j < vm.userList.length; j++) {
                //        if (vm.checked[i] == vm.userList[j].id && vm.userList[j].clientID != "" && vm.userList[j].clientID != null) {
                //            clientIDs += vm.userList[j].clientID + ',';
                //        }
                //    }
                //}

                //if (clientIDs != '') {
                //    //消息推送
                //    $http({
                //        method: 'POST',
                //        url: 'http://61.162.225.227:8001/api/Values/ToMessage',
                //        params: {
                //            title: "消息通知",
                //            content: vm.remark,
                //            clientIDs: clientIDs
                //        }
                //    }).then(function successCallback(response) {
                //        // 请求成功执行代码
                //    }, function errorCallback(response) {
                //        // 请求失败执行代码
                //    });
                //}

                notificationService.batchCreateNotification($.extend({ userIDs: [], remark: vm.remark, title: vm.title, url:'http://61.162.225.227:8001/api/Values/ToMessage' }, vm.requestParams)).then(function () {
                    abp.notify.info(app.localize('SavedSuccessfully'));
                    $uibModalInstance.close();
                }).finally(function () {
                    vm.saving = false;
                });
            };
            //取消关闭页面
            vm.cancel = function () {
                $uibModalInstance.dismiss();
            };

            //初始化页面
            function init() {

                //绑定用户控件
                //userService.getAllUsersByWhereAndRole({}).then(function (result) {
                //    vm.userList = result.data.items;
                //});

                vm.bindTown();
            }

            //绑定镇控件
            vm.bindTown = function () {
                areaService.getAreaByLevelAsync({ id: 0 }).then(function (result) {
                    vm.townList = result.data.items;
                    if (vm.townID > 0) {
                        vm.requestParams.townID = vm.townID + '';
                        vm.bindArea(vm.townID);
                    }
                });
            };

            //绑定区
            vm.bindArea = function (townID) {
                areaService.getAreaByLevelAsync({ id: townID }).then(function (result) {
                    vm.areaList = result.data.items;
                    if (vm.areaID > 0) {
                        vm.requestParams.areaID = vm.areaID + '';
                        vm.bindVillage(vm.areaID);
                    } else {
                        vm.requestParams.areaID = vm.areaList[0].id;
                    }
                });
            };

            //绑定村
            vm.bindVillage = function (areaID) {
                areaService.getAreaByLevelAsync({ id: areaID }).then(function (result) {
                    vm.villageList = result.data.items;
                    if (vm.villageID > 0) {
                        vm.requestParams.villageID = vm.villageID + '';
                    } else {
                        vm.requestParams.villageID = vm.villageList[0].id;
                    }
                });
            };
            //执行初始化方法
            init();
        }
    ]);
})();